/**
 * 
 */
package com.cnhaimei.com;

import android.view.View;
import android.view.View.OnClickListener;

/**
 * 页面管理
 * 
 * @author Jin001.Wu@gmail.com <br>
 * 
 */
public class PageManager implements OnClickListener {
	private View prePage;
	private View nextPage;
	private View firstPage;
	private View lastPage;
	private int currentIndex;
	private int totalPages;
	private PageInfoListener listener;
	private boolean hiderInvalView;

	public PageManager() {
		this(0, 0);
	}
	
	public PageManager(boolean hiderInvalView) {
		this(0, 0);
		this.hiderInvalView = hiderInvalView;
	}

	public PageManager(int currentIndex, int totalPages) {
		this.currentIndex = currentIndex;
		this.totalPages = totalPages;
	}

	public void setPageButtonView(View prePage, View nextPage, View firstPage,
			View lastPage) {
		this.prePage = prePage;
		this.nextPage = nextPage;
		this.firstPage = firstPage;
		this.lastPage = lastPage;
		setOnclick();
		hidePageView();
	}

	public void setPageInfo(int currentIndex, int totalPages) {
		setPageInfo(currentIndex, totalPages, true);
	}

	public void setPageInfo(int currentIndex, int totalPages, boolean notify) {
		if (totalPages <= 0) {
			hidePageView();
			if (listener != null && notify) {
				listener.onPageIndexChangedError(this.currentIndex,
						currentIndex, totalPages);
			}
			return;
		}
		this.totalPages = totalPages;
		setCurrentIndex(currentIndex, notify);
	}

	public void setCurrentIndex(int pageIndex, boolean notify) {
		int pre = this.currentIndex;
		int current = pageIndex;
		if (!hiderInvalView) {
			if (current < 0) {
				current = Math.max(0, totalPages - 1);
			} else if (current >= totalPages) {
				current = 0;
			}
		}
		if ((current | totalPages) == 0) {
			if (listener != null && notify) {
				listener.onPageIndexChangedError(pre, current, totalPages);
			}
			return;
		}
		if (hiderInvalView) {
			if (current < 0 || current >= totalPages) {
				if (listener != null && notify) {
					listener.onPageIndexChangedError(pre, current, totalPages);
				}
				return;
			}
		}
		this.currentIndex = current;//pageIndex;
		refreshPageView();
		if (listener != null && notify) {
			listener.onPageIndexChanged(pre, currentIndex, totalPages);
		}
	}

	public void moveToIndex(int pageIndex) {
		setCurrentIndex(pageIndex, true);
	}

	public int getCurrentIndex() {
		return currentIndex;
	}

	public int getTotalPage() {
		return totalPages;
	}

	private void refreshPageView() {
		hidePageView();
		if (!hiderInvalView) {
			return;
		}
		if (currentIndex > 0) {
			if (prePage != null) {
				prePage.setVisibility(View.VISIBLE);
			}
			if (firstPage != null) {
				firstPage.setVisibility(View.VISIBLE);
			}
		}

		if (currentIndex < totalPages - 1) {
			if (nextPage != null) {
				nextPage.setVisibility(View.VISIBLE);
			}
			if (lastPage != null) {
				lastPage.setVisibility(View.VISIBLE);
			}
		}
	}

	private void hidePageView() {
		if (!hiderInvalView) {
			return;
		}
		if (prePage != null) {
			prePage.setVisibility(View.INVISIBLE);
		}
		if (nextPage != null) {
			nextPage.setVisibility(View.INVISIBLE);
		}
		if (firstPage != null) {
			firstPage.setVisibility(View.INVISIBLE);
		}
		if (lastPage != null) {
			lastPage.setVisibility(View.INVISIBLE);
		}
	}

	public PageInfoListener getListener() {
		return listener;
	}

	public void setListener(PageInfoListener listener) {
		this.listener = listener;
	}

	private void setOnclick() {
		if (prePage != null) {
			prePage.setOnClickListener(this);
		}
		if (nextPage != null) {
			nextPage.setOnClickListener(this);
		}
		if (firstPage != null) {
			firstPage.setOnClickListener(this);
		}
		if (lastPage != null) {
			lastPage.setOnClickListener(this);
		}
	}

	public static interface PageInfoListener {
		void onPageIndexChanged(int prePageIndex, int currentPageIndex,
				int totalPages);

		void onPageIndexChangedError(int prePageIndex, int currentPageIndex,
				int totalPages);
	}

	public void onClick(View v) {
		if (v == nextPage) {
			moveToIndex(this.currentIndex + 1);
		} else if (v == prePage) {
			moveToIndex(this.currentIndex - 1);
		} else if (v == firstPage) {
			moveToIndex(0);
		} else if (v == lastPage) {
			moveToIndex(totalPages - 1);
		}

	}
}
